# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
from .database import MySql
import time


class NewsPipeline(object):
    db = MySql()

    def process_item(self, item, spider):
        # 根据创建表格式 网站_NEWS_%Y/%m/%d
        table_name = "{}_NEWS_{}".format(item['websites'], time.strftime("%Y/%m/%d", time.localtime()))
        create_table = """
        CREATE TABLE
        IF NOT EXISTS `{table_name}` (
            id INT auto_increment PRIMARY KEY,
            time time NOT NULL,
            news text
        ) ENGINE = INNODB DEFAULT CHARSET = utf8;
        """.format(table_name=table_name)
        self.db.execute(create_table)
        time_ = item["time"][0]
        news = ""
        for i in item["news"]:
            news += i
        # 插入新闻数据
        insert = """
        insert into `{table_name}`(time,news) values('{time}','{news}')
        """.format(table_name=table_name, time=time_, news=news)
        print(insert)
        self.db.execute(insert)

        # return item
